<?php
declare(strict_types=1);
/**
 * MineAdmin is committed to providing solutions for quickly building web applications
 * Please view the LICENSE file that was distributed with this source code,
 * For the full copyright and license information.
 * Thank you very much for using MineAdmin.
 *
 * @Author X.Mo<root@imoi.cn>
 * @Link   https://gitee.com/xmo/MineAdmin
 */

namespace App\System\Mapper;

use App\System\Model\SystemPayLog;
use Hyperf\Database\Model\Builder;
use Mine\Abstracts\AbstractMapper;

/**
 * 支付记录Mapper类
 */
class SystemPayLogMapper extends AbstractMapper
{
    /**
     * @var SystemPayLog
     */
    public $model;

    public function assignModel()
    {
        $this->model = SystemPayLog::class;
    }

    /**
     * 搜索处理器
     * @param Builder $query
     * @param array $params
     * @return Builder
     */
    public function handleSearch(Builder $query, array $params): Builder
    {
        
        // 订单类型
        if (!empty($params['order_type'])) {
            $query->where('order_type', '=', $params['order_type']);
        }

        // 支付方式
        if (!empty($params['pay_type'])) {
            $query->where('pay_type', '=', $params['pay_type']);
        }

        // 支付通道，0为直连商户
        if (!empty($params['channel_id'])) {
            $query->where('channel_id', '=', $params['channel_id']);
        }

        // 订单金额
        if (!empty($params['amount'])) {
            $query->where('amount', '=', $params['amount']);
        }

        // 退款金额
        if (!empty($params['refund_amount'])) {
            $query->where('refund_amount', '=', $params['refund_amount']);
        }

        // 状态
        if (!empty($params['status'])) {
            $query->where('status', '=', $params['status']);
        }

        // 退款时间
        if (!empty($params['refund_at']) && is_array($params['refund_at']) && count($params['refund_at']) == 2) {
            $query->whereBetween(
                'refund_at',
                [ $params['refund_at'][0], $params['refund_at'][1] ]
            );
        }

        // 支付时间
        if (!empty($params['paid_at']) && is_array($params['paid_at']) && count($params['paid_at']) == 2) {
            $query->whereBetween(
                'paid_at',
                [ $params['paid_at'][0], $params['paid_at'][1] ]
            );
        }

        // 关闭时间
        if (!empty($params['expired_at']) && is_array($params['expired_at']) && count($params['expired_at']) == 2) {
            $query->whereBetween(
                'expired_at',
                [ $params['expired_at'][0], $params['expired_at'][1] ]
            );
        }

        // 接口返回信息摘要
        if (!empty($params['message'])) {
            $query->where('message', 'like', '%'.$params['message'].'%');
        }

        return $query;
    }
}